package Database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Hashtable;

import Database.Recs.AccountRec;
import Database.Recs.CategoryRec;

public class GetDbFunctions {

	public static ArrayList<CategoryRec> getCategory()
	{
		Connection con;
		PreparedStatement ps;
		ResultSet rs;
		String statement;
		ArrayList<CategoryRec> result = null;
		CategoryRec tmp;
		
		try
		{
			con = Connector.getConnection();
			
			statement = "SELECT * FROM CATEGORY ORDER BY treeStructBuilder";
			ps = con.prepareStatement(statement);
			
			rs = ps.executeQuery();
			
			result = new ArrayList<CategoryRec>();
			
			while(rs.next())
			{
				tmp = new CategoryRec(rs);
				result.add(tmp);
			}
			
			ps.close();
			con.close();
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		
		return result;
	}
	
	public static Hashtable<String, CategoryRec> getCategoryHash()
	{
		Connection con;
		PreparedStatement ps;
		ResultSet rs;
		String statement;
		Hashtable<String, CategoryRec> result = null;
		CategoryRec tmp;
		
		try
		{
			con = Connector.getConnection();
			
			statement = "SELECT * FROM CATEGORY";
			ps = con.prepareStatement(statement);
			
			rs = ps.executeQuery();
			
			result = new Hashtable<String, CategoryRec>();
			
			while(rs.next())
			{
				tmp = new CategoryRec(rs);
				result.put(tmp.getTreeStructBuilder(),tmp);
			}
			
			ps.close();
			con.close();
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		
		return result;
	}
	
	public static ArrayList<AccountRec> getAccounts()
	{
		Connection con;
		PreparedStatement ps;
		ResultSet rs;
		String statement;
		ArrayList<AccountRec> result = null;
		AccountRec tmp;
		
		try
		{
			con = Connector.getConnection();
			
			statement = "SELECT * FROM ACCOUNT";
			ps = con.prepareStatement(statement);
			
			rs = ps.executeQuery();
			
			result = new ArrayList<AccountRec>();
			
			while(rs.next())
			{
				tmp = new AccountRec(rs);
				result.add(tmp);
			}
			
			ps.close();
			con.close();
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		
		return result;
	}
}
